﻿/*
 * Created by SharpDevelop.
 * User: CrOnO
 * Date: 03/10/2011
 * Time: 09:33 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.IO;
using System.Web;
using Business_Logic;
using Business_Entity;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace PetSoft.GUI.RRHH
{
	/// <summary>
	/// Description of GUIMostrarReporteAsistencia.
	/// </summary>
	public partial class GUIMostrarReporteAsistencia : Form
	{
		private List<string[]> lista;
		
		public GUIMostrarReporteAsistencia()
		{
			try
			{
				//
				// The InitializeComponent() call is required for Windows Forms designer support.
				//
				InitializeComponent();
				
				//
				// TODO: Add constructor code after the InitializeComponent() call.
				//
			}
			catch (Exception ex)
			{
				MessageBox.Show("Error: " + ex,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}
		
		void exportarClick(object sender, EventArgs e)
		{
			if (MessageBox.Show("¿Está seguro que desea exportar este reporte?", "Pregunta", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
			{
				try
				{
					int cont = 1;
					String path = "..\\ReporteAsistencia.pdf";
					while (true)
					{
						if (File.Exists(path))
						{
							path = "..\\ReporteAsistencia" + cont.ToString() + ".pdf";
							cont++;
						}
						else
						{
							break;
						}
					}	
					
					Document doc = new Document(PageSize.A4.Rotate());
					doc.AddCreationDate();
					PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create));
					doc.Open();
					iTextSharp.text.Font fntTit = FontFactory.GetFont(FontFactory.COURIER_BOLD, 18, BaseColor.WHITE);
					iTextSharp.text.Font fntTab = FontFactory.GetFont(FontFactory.COURIER, 8, BaseColor.BLACK);
					iTextSharp.text.Font fntRes = FontFactory.GetFont(FontFactory.COURIER, 7);
					PdfPTable tab = new PdfPTable(8);
					tab.TotalWidth = 820f;
					float[] widths = new float[] { 40f, 90f, 90f, 90f, 90f, 90f, 90f, 90f};
					tab.SetWidths(widths);
					tab.LockedWidth = true;
					
					PdfPCell cell;
					iTextSharp.text.Image img;
				
					cell = new PdfPCell(new Phrase(""));
					cell.Colspan = 3;
					cell.HorizontalAlignment = 1;
					cell.BackgroundColor = BaseColor.WHITE;
					cell.BorderWidth = 0;
					tab.AddCell(cell);
					//AQUI VA EL LOGO DE PETSOFT!!!!!!!!!!!////////////////////////
					/////////////////////////////////////////////////////////////
					img = Utils.getEncabezadoReporte();
					cell = new PdfPCell(img, false);
					cell.Colspan = 2;
					cell.Border = 0;
					cell.HorizontalAlignment = 0;
					tab.AddCell(cell);
					//////////////////////////////////////////////////////
					/////////////////////////////////////////////////////////
					cell = new PdfPCell(new Phrase(""));
					cell.Colspan = 6;
					cell.HorizontalAlignment = 1;
					cell.BackgroundColor = BaseColor.WHITE;
					cell.BorderWidth = 0;
					tab.AddCell(cell);
					
					cell = new PdfPCell(new Phrase("REPORTE DE ASISTENCIA",fntTit));
					cell.Colspan = 8;
					cell.HorizontalAlignment = 1;
					cell.BackgroundColor = BaseColor.BLACK;
					tab.AddCell(cell);
					PdfPCell cell2 = new PdfPCell(new Phrase("ID",fntTab));
					cell2.HorizontalAlignment = Element.ALIGN_CENTER;
					cell2.VerticalAlignment = Element.ALIGN_CENTER;
					tab.AddCell(cell2);
					cell2 = new PdfPCell(new Phrase("Nombre",fntTab));
					cell2.HorizontalAlignment = Element.ALIGN_CENTER;
					cell2.VerticalAlignment = Element.ALIGN_CENTER;
					tab.AddCell(cell2);
					cell2 = new PdfPCell(new Phrase("Apellido Paterno",fntTab));
					cell2.HorizontalAlignment = Element.ALIGN_CENTER;
					cell2.VerticalAlignment = Element.ALIGN_CENTER;
					tab.AddCell(cell2);
					cell2 = new PdfPCell(new Phrase("Apellido Materno",fntTab));
					cell2.HorizontalAlignment = Element.ALIGN_CENTER;
					cell2.VerticalAlignment = Element.ALIGN_CENTER;
					tab.AddCell(cell2);
					cell2 = new PdfPCell(new Phrase("Documento de Identidad",fntTab));
					cell2.HorizontalAlignment = Element.ALIGN_CENTER;
					cell2.VerticalAlignment = Element.ALIGN_CENTER;
					tab.AddCell(cell2);
					cell2 = new PdfPCell(new Phrase("Día de Asistencia",fntTab));
					cell2.HorizontalAlignment = Element.ALIGN_CENTER;
					cell2.VerticalAlignment = Element.ALIGN_CENTER;
					tab.AddCell(cell2);
					cell2 = new PdfPCell(new Phrase("Hora de Entrada",fntTab));
					cell2.HorizontalAlignment = Element.ALIGN_CENTER;
					cell2.VerticalAlignment = Element.ALIGN_CENTER;
					tab.AddCell(cell2);
					cell2 = new PdfPCell(new Phrase("Hora de Salida",fntTab));
					cell2.HorizontalAlignment = Element.ALIGN_CENTER;
					cell2.VerticalAlignment = Element.ALIGN_CENTER;
					tab.AddCell(cell2);
					foreach (string[] fila in lista) {
						cell2 = new PdfPCell(new Phrase(fila[0],fntRes));
						cell2.HorizontalAlignment = Element.ALIGN_CENTER;
						cell2.VerticalAlignment = Element.ALIGN_CENTER;
						tab.AddCell(cell2);
						cell2 = new PdfPCell(new Phrase(fila[1],fntRes));
						cell2.HorizontalAlignment = Element.ALIGN_CENTER;
						cell2.VerticalAlignment = Element.ALIGN_CENTER;
						tab.AddCell(cell2);
						cell2 = new PdfPCell(new Phrase(fila[2],fntRes));
						cell2.HorizontalAlignment = Element.ALIGN_CENTER;
						cell2.VerticalAlignment = Element.ALIGN_CENTER;
						tab.AddCell(cell2);
						cell2 = new PdfPCell(new Phrase(fila[3],fntRes));
						cell2.HorizontalAlignment = Element.ALIGN_CENTER;
						cell2.VerticalAlignment = Element.ALIGN_CENTER;
						tab.AddCell(cell2);
						cell2 = new PdfPCell(new Phrase(fila[4],fntRes));
						cell2.HorizontalAlignment = Element.ALIGN_CENTER;
						cell2.VerticalAlignment = Element.ALIGN_CENTER;
						tab.AddCell(cell2);
						cell2 = new PdfPCell(new Phrase(fila[5],fntRes));
						cell2.HorizontalAlignment = Element.ALIGN_CENTER;
						cell2.VerticalAlignment = Element.ALIGN_CENTER;
						tab.AddCell(cell2);
						cell2 = new PdfPCell(new Phrase(fila[6],fntRes));
						cell2.HorizontalAlignment = Element.ALIGN_CENTER;
						cell2.VerticalAlignment = Element.ALIGN_CENTER;
						tab.AddCell(cell2);
						cell2 = new PdfPCell(new Phrase(fila[7],fntRes));
						cell2.HorizontalAlignment = Element.ALIGN_CENTER;
						cell2.VerticalAlignment = Element.ALIGN_CENTER;
						tab.AddCell(cell2);
					}
					doc.Add(tab);
					doc.Close();
//					MessageBox.Show("Exportación exitosa", "Aviso");
					System.Diagnostics.Process.Start("AcroRd32.exe",path);
					this.Close();
				}
				catch (Exception ex)
				{
					MessageBox.Show("Exportación fallida: " + ex,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
				}
			}	
		}

		void GUIMostrarReporteAsistenciaLoad(object sender, EventArgs e)
		{
			try
			{
				if (this.lista != null)
				{
					this.resultadosDGV.AllowUserToAddRows = true;
					this.resultadosDGV.Rows.Clear();
					foreach(string[] fila in lista)
					{
						this.resultadosDGV.Rows.Add(fila);
					}
					this.resultadosDGV.AllowUserToAddRows = false;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show("Error: " + ex,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}
		
				
		public List<string[]> Lista
		{
			get { return lista; }
			set { lista = value; }
		}
	}
}
